from machine import UART, Pin
import uasyncio as asyncio
import utime


class UartLcd:
    
    def __init__(self, lcd_tx, lcd_rx):
        print("-" * 36)
        print("init uart lcd ...")
        self.uart = self._init_uart(lcd_tx, lcd_rx)
        self._init_screen()
        print("-" * 36)

    def uart_interrupt_handler(self, uart):
        # 处理串口中断的代码
        data = uart.read()  # 读取接收到的数据
        print("UartLcd.uart Received data:", data)

    def _init_uart(self, lcd_tx, lcd_rx):
        uart = None
        try:
            uart = UART(2, baudrate=9600, tx=Pin(lcd_tx), rx=Pin(lcd_rx))
            asyncio.create_task(self.uart_reader())
        except Exception as e:
            print("UartLcd._init_uart failed:", e)
        return uart

    def _init_screen(self):
        self.show_title("DIGITS POWER VER:0.8")
        self.show_voltage(0.00)
        self.show_voltage_unit("V")
        self.show_voltage_info("|---00.00---|")
        self.show_current(0.00)
        self.show_current_unit("A")
        self.show_current_info("|---00.00---|")
        self.show_power(0.00)
        self.show_power_unit("W")
        self.show_power_info("|---00.00---|")
        self.show_state_txt1("[STOP]")
        self.show_state_txt2("[-CV-]")
        self.show_state_txt3("[UNLK]")
        self.show_state_txt4("[-OFF]")
        self.show_info_txt0("//////////////////////")
        self.show_info_txt1("//////////////////////")
        self.show_info_txt2("//////////////////////")
        self.show_info_txt3("//////////////////////")
        self.show_info_txt4("//////////////////////")
        self.show_info_txt5("//////////////////////")
        self.show_info_txt6("//////////////////////")
        self.show_info_txt7("//////////////////////")
        self.show_info_txt8("//////////////////////")
        self.show_info_txt9("//////////////////////")
        self.show_curr_33(0.00)
        self.show_curr_33_info("/////////////")
        self.show_curr_A5(0.00)
        self.show_curr_A5_info("/////////////")
        #self.show_curr_S5(0.00)
        #self.show_curr_S5_info("/////////////")
        self.show_curr_A12(0.00)
        self.show_curr_A12_info("/////////////")
        self.show_curr_S12(0.00)
        self.show_curr_S12_info("/////////////")
        self.show_curr_Adj_info_cvcc("CV")
        self.show_curr_Adj_info_onoff("OFF")
        self.show_curr_Adj_left(0.00)
        self.show_curr_Adj_right(0.00)
        self.show_mode_index(0)
        self.show_out_activate_index(2)

        
    async def uart_reader(self):
        while True:
            #print("UartLcd.uart_reader...")
            if self.uart.any():
                data = self.uart.read()
                # uart_callback(data)
                print("UartLcd.received:", data)
            await asyncio.sleep_ms(100)

    def _send(self, data):
        tail = bytearray.fromhex('ff' * 3)
        data = bytearray(data.encode("ascii"))
        buf = data + tail
#         print("UartLcd._send:", buf)
        self.uart.write(buf)

    def show_voltage(self, vol, digits=2):
        cmd = "main.x0.val="+str(int(vol * 10 ** digits))
        self._send(cmd)
        return True
    
    def show_voltage_unit(self, val_str):
        cmd = "main.t10.txt=\""+val_str+"\""
        self._send(cmd)
        return True
    
    def show_voltage_info(self, val_str):
        cmd = "main.t17.txt=\""+val_str+"\""
        self._send(cmd)
        return True

    def show_current(self, cur, digits=2):
        cmd = "main.x1.val=" + str(int(cur * 10 ** digits))
        self._send(cmd)
        #cmd = "main.x17.val=" + str(int(cur * 10 ** digits))
        #self._send(cmd)
        return True
    
    def show_current_unit(self, val_str):
        cmd = "main.t11.txt=\""+val_str+"\""
        self._send(cmd)
        return True
    
    def show_current_info(self, val_str):
        cmd = "main.t18.txt=\""+val_str+"\""
        self._send(cmd)
        return True

    def show_power(self, power, digits=1):
        cmd = "main.x2.val=" + str(int(power * 10 ** digits))
        self._send(cmd)
        return True
    
    def show_power_unit(self, val_str):
        cmd = "main.t12.txt=\"" + val_str + "\""
        self._send(cmd)
        return True
    
    def show_power_info(self, val_str):
        cmd = "main.t19.txt=\"" + val_str + "\""
        self._send(cmd)
        return True
    
    def show_state_txt1(self, val_str):
        cmd = "main.t13.txt=\"" + val_str +"\""
        self._send(cmd)
        return True
    
    def show_state_txt2(self, val_str):
        cmd = "main.t14.txt=\"" + val_str + "\""
        self._send(cmd)
        return True
                   
    def show_state_txt3(self, val_str):
        cmd = "main.t15.txt=\"" + val_str + "\""
        self._send(cmd)
        return True
    
    def show_state_txt4(self, val_str):
        cmd = "main.t16.txt=\"" + val_str + "\""
        self._send(cmd)
        return True
                             
    def show_info_txt0(self, val_str):
        cmd = "main.t0.txt=\"" + val_str + "\""
        self._send(cmd)
        return True
    
    def show_info_txt1(self, val_str):
        cmd = "main.t1.txt=\"" + val_str + "\""
        self._send(cmd)
        return True
    
    def show_info_txt2(self, val_str):
        cmd = "main.t2.txt=\"" + val_str + "\""
        self._send(cmd)
        return True
    
    def show_info_txt3(self, val_str):
        cmd = "main.t3.txt=\"" + val_str + "\""
        self._send(cmd)
        return True
                             
    def show_info_txt4(self, val_str):
        cmd = "main.t4.txt=\"" + val_str + "\""
        self._send(cmd)
        return True
    
    def show_info_txt5(self, val_str):
        cmd = "main.t5.txt=\"" + val_str + "\""
        self._send(cmd)
        return True
    
    def show_info_txt6(self, val_str):
        cmd = "main.t6.txt=\"" + val_str + "\""
        self._send(cmd)
        return True
    
    def show_info_txt7(self, val_str):
        cmd = "main.t7.txt=\"" + val_str + "\""
        self._send(cmd)
        return True
    
    def show_info_txt8(self, val_str):
        cmd = "main.t8.txt=\"" + val_str + "\""
        self._send(cmd)
        return True
                             
    def show_info_txt9(self, val_str):
        cmd = "main.t9.txt=\"" + val_str + "\""
        self._send(cmd)
        return True
   
    def show_curr_33(self, cur, digits=2):
        cmd = "main.x12.val=" + str(int(cur * 10 ** digits))
        self._send(cmd)
        return True
    
    def show_curr_33_info(self, val_str):
        cmd = "main.t20.txt=\"" + val_str + "\""
        self._send(cmd)
        return True
    
    
    def show_curr_A5(self, cur, digits=2):
        cmd = "main.x13.val=" + str(int(cur * 10 ** digits))
        self._send(cmd)
        return True
    
    def show_curr_A5_info(self, val_str):
        cmd = "main.t21.txt=\"" + val_str + "\""
        self._send(cmd)
        return True
                             

    
    def show_curr_A12(self, cur, digits=2):
        cmd = "main.x16.val=" + str(int(cur * 10 ** digits))
        self._send(cmd)
        return True

    def show_curr_A12_info(self, val_str):
        cmd = "main.t24.txt=\"" + val_str + "\""
        self._send(cmd)
        return True
    
    def show_curr_S12(self, cur, digits=2):
        cmd = "main.x17.val=" + str(int(cur * 10 ** digits))
        self._send(cmd)
        return True
    
    def show_curr_S12_info(self, val_str):
        cmd = "main.t25.txt=\"" + val_str + "\""
        self._send(cmd)
        return True
    
    def show_curr_Adj_left(self, cur, digits=2):
        cmd = "main.x14.val=" + str(int(cur * 10 ** digits))
        self._send(cmd)
        return True

    def show_curr_Adj_right(self, cur, digits=2):
        cmd = "main.x15.val=" + str(int(cur * 10 ** digits))
        self._send(cmd)
        return True
    
    def show_curr_Adj_info_cvcc(self, val_str):
        cmd = "main.t22.txt=\"" + val_str + "\""
        self._send(cmd)
        return True

    def show_curr_Adj_info_onoff(self, val_str):
        cmd = "main.t23.txt=\"" + val_str + "\""
        self._send(cmd)
        return True

    def show_mode_index(self, mode_index):
        cmd = "main.n1.val=" + str(mode_index)
        self._send(cmd)
        return True

    def show_out_activate_index(self, mode_index):
        cmd = "main.va_act.val=" + str(mode_index)
        self._send(cmd)
        return True

    def show_title(self, val_str):
        cmd = "main.g0.txt=\"" + val_str + "\""
        self._send(cmd)
        return True

if __name__ == "__main__":
    tx = 16#19
    rx = 17#18
    #uart = UART(2, baudrate=9600, tx=Pin(tx), rx=Pin(rx))
    #cmd = "main.x0.val="+str(15 * 10 ** 2)
    #tail = bytearray.fromhex('ff' * 3)
    #data = bytearray(cmd.encode("ascii"))
    #buf = data + tail
    #print("UartLcd._send:", buf)
    #uart.write(buf)
    #while True:
    #    if uart.any() > 0:
    #        data = uart.read()
    #        print("Received data:", data)
    #    utime.sleep(1)
    lcd = UartLcd(tx, rx)
    lcd.show_voltage(20)
    lcd.show_current(10)
    lcd.show_power(200)
    lcd.show_state_txt1("STOP")
    
    

